package study.ct.com.retrofit_rxjava.net.extension;

import android.util.Log;

import java.io.IOError;
import java.io.IOException;

import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import study.ct.com.retrofit_rxjava.BuildConfig;
import study.ct.com.retrofit_rxjava.net.retrofit.NetProvider;
import study.ct.com.retrofit_rxjava.net.retrofit.RequestHandler;

/**
 * Created by ct on 2017/10/11 0011.
 */

public class BaseNetProvider implements NetProvider {

    private static final long CONNECT_TIME_OUT = 30;
    private static final long READ_TIME_OUT = 180;
    private static final long WRITE_TIME_OUT = 30;

    private static final String TAG = "BaseNetProvider";

    @Override
    public long configConnectTimeoutSecs() {
        return CONNECT_TIME_OUT;
    }

    @Override
    public long configReadTimeoutSecs() {
        return READ_TIME_OUT;
    }

    @Override
    public long configWriteTimeoutSecs() {
        return WRITE_TIME_OUT;
    }

    @Override
    public CookieJar configCookie() {
        return null;
    }

    @Override
    public void configHttps(OkHttpClient.Builder builder) {

    }

    @Override
    public RequestHandler configHandler() {
        return new RequestHandler() {
            @Override
            public Request onBeforeRequest(Request request, Interceptor.Chain chain) {
                Log.i(TAG, "configHandler onBeforeRequest: ====");
                 return chain.request().newBuilder()
                        .addHeader("X-Auth-Token", "")//自定义token 放在协议header 里面
                        .addHeader("Authorization", "")
                        .build();
            }

            @Override
            public Response onAfterRequest(Response response, Interceptor.Chain chain) throws IOException {
                Log.i(TAG, "configHandler onAfterRequest: ========"+response.body().toString());
                /*ApiException e = null;
                if (401 == response.code()) {
                    throw new ApiException("登录已过期,请重新登录!");
                } else if (403 == response.code()) {
                    throw new ApiException("禁止访问!");
                } else if (404 == response.code()) {
                    throw new ApiException("链接错误");
                } else if (503 == response.code()) {
                    throw new ApiException("服务器升级中!");
                } else if (500 == response.code()) {
                    throw new ApiException("服务器内部错误!");
                }*/

                if (401 == response.code()) {
                    throw new IOException("登录已过期,请重新登录!");
                } else if (403 == response.code()) {
                    throw new IOException("禁止访问!");
                } else if (404 == response.code()) {
                    throw new IOException("链接错误");
                } else if (503 == response.code()) {
                    throw new IOException("服务器升级中!");
                } else if (500 == response.code()) {
                    throw new IOException("服务器内部错误!");
                }
                return response;
            }
        };
    }

    @Override
    public Interceptor[] configInterceptors() {
        return new Interceptor[0];
    }

    @Override
    public boolean configLogEnable() {
        return true;
    }


}
